package com.scm.biz.service._impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scm.biz.bean.BusinessConstant;
import com.scm.biz.bean.Constant;
import com.scm.biz.dao.BusinessConstantDao;
import com.scm.biz.dao.ConstantDao;
import com.scm.biz.query.customer.ConstantQuery;
import com.scm.biz.service.BusinessConstantService;
import com.scm.utils.Const;

/**
 * 业务分类服务类实现类
 * @author Administrator
 *
 */
@Service("businessConstantService")
public class BusinessConstantServiceImpl implements BusinessConstantService{
	
	@Resource
	private BusinessConstantDao businessConstantDao;
	
	@Resource
	private ConstantDao constantDao;
	
	@Override
	public void save(String label, long customerId, String category) {
		if(label==null||label.trim().isEmpty()){
			return;
		}
		String[] labels=label.split(Const.SPLIT);
		ConstantQuery query=new ConstantQuery();
		query.setCategory(category);
		for(String name:labels){
			query.setName(name);
			Constant c=new Constant();
			List<Constant> list=constantDao.selectByQuery(query);
			BusinessConstant bc=new BusinessConstant();
			if(list==null||list.isEmpty()){
				c.setCategory(category);
				c.setName(name);
				c.setCreated_at(new Date());
				constantDao.insert(c);
				bc.setConstant_id(c.getId());
			}else{
				bc.setConstant_id(list.get(0).getId());
			}
			bc.setBusiness_id(customerId);
			bc.setCategory(category);
			bc.setCreated_at(new Date());
			businessConstantDao.insert(bc);
		}
	}
	
	
}
